<?php
define("SERVER_VERSION",     0x00094100);
define("PROTOCOL_VERSION",   0x00010D00);
define("OPTIONS_NUM",        62);

function MakeWord($a, $b){
  return $a | $b << 8;
}

function MakeInt($a, $b, $c, $d){
  $wA = MakeWord($a, $b);
  $wB = MakeWord($c, $d);
  return $wA | $wB << 16;
}

function HiWord($L){
  return $L >> 16;
}

function LoWord($L){
  return $L & 0x0000FFFF;
}

function HiByte($W){
 return $W >> 8;
}

function LoByte($W){
  return $W & 0xFF;
}

function ParserString(&$data, &$strValue){
  if(strlen($data) < 2)
    return false;

  $len = ord($data[0]) * 256 + ord($data[1]);
  if(strlen($data)-2 < $len)
    return false;
  $data = substr($data, 2);
  $strValue = substr($data, 0, $len);
  $data = substr($data, $len);
  return true;
}

function ParserInt(&$data, &$intValue){
  if(strlen($data) < 4) 
     return false;

  $intValue = MakeInt(ord($data[0]), ord($data[1]), ord($data[2]), ord($data[3]));
  $data = substr($data, 4);
}

function ParserWord(&$data, &$wordValue){
  if(strlen($data) < 2)
    return false;
  
  $wordValue = ord($data[0]) * 256 + ord($data[1]);
  $data = substr($data, 2);
}

function ParserWord2(&$data, &$wordValue){
  if(strlen($data) < 2)
    return false;

  //$wordValue = ord($data[0]) << 8 + ord($data[1]);
  $wordValue = ord($data[0]) * 256 + ord($data[1]);
  $data = substr($data, 2);
}

function ParserByte(&$data, &$byteValue){
  if(strlen($data) < 1)
    return false;

  $byteValue = ord($data[0]);
  $data = substr($data, 1);
}

function Debug($msg){
  $msg = $msg . '<br>';
  switch(func_num_args()){
  case 1:
    printf($msg);
    break;
  case 2:
    printf($msg, func_get_arg(1));
    break;
  case 3:
    printf($msg, func_get_arg(1), func_get_arg(2));
    break;
  case 4:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3));
    break;
  case 5:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4));
    break;
  case 6:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4), func_get_arg(5));
    break;
  }
}

function Info($msg){
  switch(func_num_args()){
  case 1:
    printf($msg);
    break;
  case 2:
    printf($msg, func_get_arg(1));
    break;
  case 3:
    printf($msg, func_get_arg(1), func_get_arg(2));
    break;
  case 4:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3));
    break;
  case 5:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4));
    break;
  case 6:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4), func_get_arg(5));
    break;
  }
}

function Error($msg){
  switch(func_num_args()){
  case 1:
    printf($msg);
    break;
  case 2:
    printf($msg, func_get_arg(1));
    break;
  case 3:
    printf($msg, func_get_arg(1), func_get_arg(2));
    break;
  case 4:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3));
    break;
  case 5:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4));
    break;
  case 6:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4), func_get_arg(5));
    break;
  }
}

function Fatal($msg){
  switch(func_num_args()){
  case 1:
    printf($msg);
    break;
  case 2:
    printf($msg, func_get_arg(1));
    break;
  case 3:
    printf($msg, func_get_arg(1), func_get_arg(2));
    break;
  case 4:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3));
    break;
  case 5:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4));
    break;
  case 6:
    printf($msg, func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4), func_get_arg(5));
    break;
  }
}

function GetRequiredStringBufferLen($value){
  return strlen($value) + 2;
}

function FillString(&$data, &$p, $str){
  if(strlen($str) == 0){
    $data[$p++] = chr(0);
    $data[$p++] = chr(0);
    return;
  }

  $len = strlen($str);
  $data[$p++] = chr($len >> 8);
  $data[$p++] = chr($len &  0xFF);

  for($i = 0; $i < $len;$i++){
    $data[$p++] = $str[$i];
  }
}

function FillInt(&$data, &$p, $nVal){
  $data[$p++] = chr($nVal >> 24);
  $data[$p++] = chr($nVal >> 16);
  $data[$p++] = chr($nVal >> 8);
  $data[$p++] = chr($nVal &  0xFF);
}

$gBoTerminated = false;
$gCmd = "";
$gTimeOut = 10;
$gRunTime = time();
$gParams = array();
$gAdminPwd = "";

function isTerminated(){
  return $GLOBALS['gBoTerminated'];
}

function Terminate(){
  $GLOBALS['gBoTerminated'] = true;
}

function GetCmd(){
  return $GLOBALS['gCmd'];
}

function isTimeOut(){
  return time() - $GLOBALS['gRunTime'] > $GLOBALS['gTimeOut'];
}

function ParserParams(){
  /*  if($argc > 3){
    $GLOBALS['gTimeOut'] = $argv[1];
    $GLOBALS['gCmd']     = $argv[2];
    $GLOBALS['gParams']['user'] = $argv[3];
    $GLOBALS['gParams']['dir']  = $argv[4];
  }else{
    $GLOBALS['gTimeOut']        = $_REQUEST['timeout'];
    $GLOBALS['gCmd']            = $_REQUEST['cmd'];
    $GLOBALS['gParams']['user'] = $_REQUEST['user'];
    $GLOBALS['gParams']['dir']  = $_REQUEST['dir'];    
    }*/
  $GLOBALS['gTimeOut']        = $_REQUEST['timeout'];
  $GLOBALS['gCmd']            = $_REQUEST['cmd'];
  $GLOBALS['gParams']['user'] = $_REQUEST['user'];
  $GLOBALS['gParams']['dir']  = $_REQUEST['dir'];   
  $GLOBALS['gAdminPwd']       = $_REQUEST['adminpwd'];   
  
  /*  $GLOBALS['gTimeOut']        = 10;
  $GLOBALS['gCmd']            = 'chgdir';
  $GLOBALS['gParams']['user'] = "jasonao";
  $GLOBALS['gParams']['dir']  = "d:";    
  $GLOBALS['gAdminPwd']       = "123456";*/
}

?>